<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2024/2/23
 * Time: 10:13
 */

namespace app\api\controller;

use app\api\model\UUser;
use app\api\model\GoodsCollect;
use app\api\model\Goods;

class User extends Base
{
    public function __construct()
    {
        parent::__construct();

    }

    /**
     * 用户基本信息
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function userBaseInfo(){
        $data = UUser::findData(['user_id' => $this->user_id]);
        return json(['status' => 200, 'msg' => $data]);
    }

    /**
     * 提交审核
     * user_email      邮箱
     * user_xing       姓
     * user_name       名
     * user_phone      电话
     * user_company    公司
     * user_position   职位
     * @return \think\response\Json
     */
    public function commitCheck(){
        $user_email = input('user_email');
        $user_xing = input('user_xing');
        $user_name = input('user_name');
        $user_phone = input('user_phone');
        $user_company = input('user_company');
        $user_position = input('user_position');

        if(!$user_xing) return json(['status' => 0, 'msg' => $this->lanMsg->requireXing[$this->lanType]]);
        if(!$user_name) return json(['status' => 0, 'msg' => $this->lanMsg->requireMing[$this->lanType]]);
        if(!$user_phone) return json(['status' => 0, 'msg' => $this->lanMsg->requirePhone[$this->lanType]]);
        if(!$user_company) return json(['status' => 0, 'msg' => $this->lanMsg->requireCompany[$this->lanType]]);
        if(!$user_position) return json(['status' => 0, 'msg' => $this->lanMsg->requirePosition[$this->lanType]]);

        $update = [
            'user_email' => $user_email,
            'user_xing' => $user_xing,
            'user_name' => $user_name,
            'user_phone' => $user_phone,
            'user_company' => $user_company,
            'user_position' => $user_position,
            'user_status' => 1,
            'user_update_time' => initTime(),
            'user_refuse_reason' => ''
        ];

        $res = UUser::update($update, ['user_id' => $this->user_id]);
        if($res){
            return json(['status' => 200, 'msg' => $this->lanMsg->checkSuccessCommit[$this->lanType]]);
        }else{
            return json(['status' => 0, 'msg' => $this->lanMsg->systemErr[$this->lanType]]);
        }
    }

    /**
     * 编辑用户信息---H5
     * user_email      邮箱
     * user_xing       姓
     * user_name       名
     * user_phone      电话
     * user_company    公司
     * user_position   职位
     * @return \think\response\Json
     */
    public function updateBaseInfoForH5(){
        $user_email = input('user_email');
        $user_xing = input('user_xing');
        $user_name = input('user_name');
        $user_phone = input('user_phone');
        $user_company = input('user_company');
        $user_position = input('user_position');

        $update = ['user_status' => 1, 'user_update_time' => initTime()];
        if($user_email) $update['user_email'] = $user_email;
        if($user_xing) $update['user_xing'] = $user_xing;
        if($user_name) $update['user_name'] = $user_name;
        if($user_phone) $update['user_phone'] = $user_phone;
        if($user_company) $update['user_company'] = $user_company;
        if($user_position) $update['user_position'] = $user_position;

        $res = UUser::update($update, ['user_id' => $this->user_id]);
        if($res){
            return json(['status' => 200, 'msg' => $this->lanMsg->doSuccess[$this->lanType]]);
        }else{
            return json(['status' => 0, 'msg' => $this->lanMsg->systemErr[$this->lanType]]);
        }
    }

    /**
     * 编辑用户头像---H5
     * user_image      头像
     * @return \think\response\Json
     */
    public function updateUserImageForH5(){
        $user_image = input('user_image');

        $update = ['user_image' => $user_image, 'user_update_time' => initTime()];

        $res = UUser::update($update, ['user_id' => $this->user_id]);
        if($res){
            return json(['status' => 200, 'msg' => $this->lanMsg->doSuccess[$this->lanType]]);
        }else{
            return json(['status' => 0, 'msg' => $this->lanMsg->systemErr[$this->lanType]]);
        }
    }

    /**
     * 修改密码
     * oldPwd     原密码
     * pwd        新密码
     * verifyPwd  确认新密码
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function changePwd(){
        $oldPwd = input('oldPwd');
        $pwd = input('pwd');
        $verifyPwd = input('verifyPwd');

        if(!$oldPwd) return json(['status' => 0, 'msg' => $this->lanMsg->requireOldPassword[$this->lanType]]);
        if(!$pwd) return json(['status' => 0, 'msg' => $this->lanMsg->requireNewPassword[$this->lanType]]);
        if(!$verifyPwd) return json(['status' => 0, 'msg' => $this->lanMsg->verifyPassword[$this->lanType]]);
        if($pwd != $verifyPwd) return json(['status' => 0, 'msg' => $this->lanMsg->passwordNotSame[$this->lanType]]);

        $user = UUser::findData(['user_id' => $this->user_id]);
        if(md5($oldPwd) != $user['user_password']) return json(['status' => 0, 'msg' => $this->lanMsg->oldPasswordErr[$this->lanType]]);

        $update = [
            'user_password' => md5($pwd),
            'user_update_time' => initTime()
        ];

        $res = UUser::update($update, ['user_id' => $this->user_id]);
        if($res){
            return json(['status' => 200, 'msg' => $this->lanMsg->doSuccess[$this->lanType]]);
        }else{
            return json(['status' => 0, 'msg' => $this->lanMsg->systemErr[$this->lanType]]);
        }
    }

    /**
     * 我的收藏列表
     * page    页码
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function myCollect(){
        $page = input('page') ? input('page') : 1;

        $where = ['collect_user_id' => $this->user_id];
        $field = 'goods_id, goods_name_en, goods_name_es, goods_name_tp, goods_thumb, goods_price, 
        goods_cu_price, goods_intro_en, goods_intro_es, goods_intro_tp';

        $data = GoodsCollect::selectJoinData($where, $field, $page)->toArray();
        if($data){
            foreach ($data as $k => $v){
                if($v['goods_thumb']){
                    $data[$k]['goods_thumb'] = http_type() . $v['goods_thumb'];
                }
            }
            $count = GoodsCollect::countData($where);
            return json(['status' => 200, 'msg' => $data, 'count' => $count]);
        }else{
            return json(['status' => 200, 'msg' => [], 'count' => 0]);
        }
    }

    /**
     * 收藏/取消收藏
     * goods_id    商品ID
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function doCollect(){
        $goods_id = input('goods_id');

        $goodsWhere = ['goods_id' => $goods_id];
        $goodsField = 'goods_status';
        $goods = Goods::findData($goodsWhere, $goodsField);

        $collectWhere = ['collect_user_id' => $this->user_id, 'collect_goods_id' => $goods_id];
        $collect = GoodsCollect::findData($collectWhere);

        if($collect){ // 已经收藏，取消收藏
            $res = GoodsCollect::delData(['collect_id' => $collect['collect_id']]);
            $hasCollect = 2;
        }else{ // 未收藏，添加收藏
            if($goods['goods_status'] != 1){
                return json(['status' => 0, 'msg' => $this->lanMsg->goodsStatusErr[$this->lanType]]);
            }

            $insert = [
                'collect_user_id' => $this->user_id,
                'collect_goods_id' => $goods_id
            ];

            $res = GoodsCollect::create($insert);
            $hasCollect = 1;
        }
        if($res){
            return json(['status' => 200, 'msg' => $this->lanMsg->doSuccess[$this->lanType], 'hasCollect' => $hasCollect]);
        }else{
            return json(['status' => 0, 'msg' => $this->lanMsg->systemErr[$this->lanType]]);
        }
    }
}